package com.hsylient.javase.chapter14.digest;

import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.MD5;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.nio.charset.StandardCharsets;

@Slf4j
public class DigestTest {

    @Test
    public void testMD5() {
        byte[] digest = MD5.create().digest("Hsylient", StandardCharsets.UTF_8);
        String digestBase64 = Base64Encoder.encodeUrlSafe(digest);
        log.info("[Hsylient]经过MD5加密以后得到的Base64字符串为：{}", digestBase64);
        String digestHx = HexUtil.encodeHexStr(digest);
        log.info("[Hsylient]经过MD5加密以后得到的Hx字符串为：{}", digestHx);


        String digestStr = DigestUtil.digester(DigestAlgorithm.MD5).digestHex("Hsylient");
        log.info("摘要算法MD5:{}", digestStr);
    }


    @Test
    public void testHutool() {
        String sha256Digest = DigestUtil.digester(DigestAlgorithm.SHA256).digestHex("Hsylient");
        byte[] digest = DigestUtil.digester(DigestAlgorithm.SHA256).digest("Hsylient", StandardCharsets.UTF_8);
        log.info("[Hsylient]在SHA256算法下的摘要转BASE64字符串:{}", sha256Digest);
        log.info(Base64Encoder.encode(digest));
        log.info("[Hsylient]在SHA256算法下的摘要转十六进制字符串:{}", sha256Digest);
    }
}
